//
//  Utility.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/26/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "Utility.h"
#import "UIConstants.h"

@implementation Utility

#pragma mark - General methods
+ (NSInteger)getLineHeightOfFont:(UIFont *)font
{
    if (font == nil)
    {
        return 0;
    }
    
    return ceilf([@"a" sizeWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)].height);
}

+ (id)getObject:(NSArray*)objects atIndex:(NSInteger)index
{
    if (objects == nil)
    {
        return nil;
    }
    
    if (index < 0 || [objects count] <= index)
    {
        return nil;
    }
    
    return [objects objectAtIndex:index];
}



+ (CGFloat)estimateTimeWithDis:(CGFloat) dis andVelocity:(CGFloat) vel
{
    CGFloat time = (dis / vel*1.5);
    return time > SLIDING_SLOWEST_SPEED_TIME?SLIDING_SLOWEST_SPEED_TIME:time;
}

+ (NSInteger)offsetTopForViewController
{
    if (SYSTEM_VERSION_LESS_THAN(iOS_7_0))
    {
        return 0;
    }
    
    return 20;
}

+ (void)setImageNormal:(NSString*)imageNormal imageHightLight:(NSString*)imageHightLight forButton:(UIButton*)button
{
    [button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageHightLight] forState:UIControlStateHighlighted];
}

#pragma mark - Calculate size of string
+ (CGSize)getSizeOfString:(NSString*)string font:(UIFont*)font width:(CGFloat)width
{
    if (font && string && string.length > 0)
    {
        CGSize expectedSize;
        
        if (SYSTEM_VERSION_LESS_THAN(iOS_7_0))
        {
            expectedSize = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
        }
        else
        {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
            NSDictionary *attributes = @{NSFontAttributeName : font};
            [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:string attributes:attributes]];
            
            CGSize size = CGSizeMake(width, CGFLOAT_MAX);
            CGRect rect = [attributedString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
            
            expectedSize = rect.size;
            expectedSize = CGSizeMake(ceilf(expectedSize.width), ceilf(expectedSize.height));
        }
        
        return expectedSize;
    }
    
    return CGSizeZero;
}

+ (CGSize)getSizeOfString:(NSString*)string font:(UIFont*)font size:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
{
    if (font && string && string.length > 0)
    {
        CGSize expectedSize;
        
        if (SYSTEM_VERSION_LESS_THAN(iOS_7_0))
        {
            expectedSize = [string sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
        }
        else
        {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
            NSDictionary *attributes = @{NSFontAttributeName : font};
            [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:string attributes:attributes]];
            
            CGRect rect = [attributedString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
            
            expectedSize = rect.size;
            expectedSize = CGSizeMake(ceilf(expectedSize.width), ceilf(expectedSize.height));
        }
        
        return expectedSize;
    }
    
    return CGSizeZero;
}

@end
